package ipad.asia.controller;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import ipad.asia.bean.AlipayBean;
import ipad.asia.service.PayService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@RestController()
@RequestMapping("order")
public class OrderController {
    @Resource
    private PayService payService;//调用支付服务

    /*阿里支付*/
    @RequestMapping (value = "alipay")
    public String alipay(@RequestParam(value = "out_trade_no", required = false) String out_trade_no,
                       @RequestParam(value = "subject", required = false) String subject,
                       @RequestParam(value = "total_amount", required = false) String total_amount,
                       @RequestParam(value = "body", required = false) String body)throws AlipayApiException {
        AlipayBean alipayBean=new AlipayBean();
        alipayBean.setBody(body)
                .setOut_trade_no(out_trade_no)
                .setTotal_amount(new StringBuffer().append(total_amount))
                .setSubject(subject);
        System.out.println("金额："+alipayBean.getTotal_amount());
        System.out.println("商品名："+alipayBean.getSubject());
        System.out.println(alipayBean.getBody()+alipayBean.getSubject()+alipayBean.getOut_trade_no()+alipayBean);
        return payService.aliPay(alipayBean);
    }

    @Autowired
    private DiscoveryClient client;
    @GetMapping("alipay/discovery")
    public Object discover(){
        List<String> services = client.getServices();
        System.out.println("discovery->"+services);
        List<ServiceInstance> instances = client.getInstances("ALIPAY-2001");
        for (ServiceInstance instance : instances) {
            System.out.println(instance.getHost()+"/t"+
                    instance.getUri()+"/t"+
                    instance.getPort());
        }
        return this.client;
    }
}